package com.ruoyi.framework.method;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author TsingYu
 * @version 1.0
 * @date 2025/10/28 11:31
 */
public class UpdateBatchMethod extends AbstractMethod {
    // @Override
    // public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    //     final String sql = "<script>\n update %s %s \n where id in \n <foreach collection=\"list\" item=\"item\" separator=\",\" open=\"(\" close=\")\">\n #{item.id} </foreach> \n </script>";
    //     final String valueSql = prepareValuesSql(tableInfo);
    //     final String sqlResult = String.format(sql, tableInfo.getTableName(), valueSql);
    //     SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
    //     return this.addUpdateMappedStatement(mapperClass, modelClass, "updateAllBatch", sqlSource);
    // }
    //
    // private String prepareValuesSql(TableInfo tableInfo) {
    //     final StringBuilder valueSql = new StringBuilder();
    //     valueSql.append("<trim prefix=\"set\" suffixOverrides=\",\">\n");
    //     tableInfo.getFieldList().forEach(x -> {
    //         valueSql.append("<trim prefix=\"").append(x.getColumn()).append(" =(case \" suffix=\"end),\">\n");
    //         valueSql.append("<foreach collection=\"list\" item=\"item\" >\n");
    //         valueSql.append("when id=#{item.id} then ifnull(#{item.").append(x.getProperty()).append("},").append(x.getColumn()).append(")\n");
    //         valueSql.append("</foreach>\n");
    //         valueSql.append("else ").append(x.getColumn());
    //         valueSql.append("</trim>\n");
    //     });
    //     valueSql.append("</trim>\n");
    //     System.out.println("=================================999999===============================================");
    //     System.out.println(valueSql.toString());
    //     return valueSql.toString();
    // }

    public UpdateBatchMethod(String methodName) {
        super(methodName);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>";
        String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
        String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
        //log.debug("sqlResult----->{}", sqlResult);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
        // 第三个参数必须和RootMapper的自定义方法名一致
        return this.addUpdateMappedStatement(mapperClass, modelClass, "updateAllBatch", sqlSource);
    }


    //     @Override
    //     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    //         // 定义 SQL 方法名称
    //         // SqlMethod sqlMethod = SqlMethod.UPDATE;
    //         // 构建更新的 SET 语句片段
    //         String sql = "UPDATE %s SET %s WHERE %s IN (%s)";
    //         // 获取表名
    //         String tableName = tableInfo.getTableName();
    //         // 获取主键列名
    //         String keyProperty = tableInfo.getKeyProperty();
    //         String keyColumn = tableInfo.getKeyColumn();
    //
    //         // 构建 SET 部分的 SQL 脚本（动态更新非空字段）
    //         String setSql = tableInfo.getFieldList().stream()
    //                 .map(field -> field.getColumn() + " = CASE " + keyColumn + "\n"
    //                         + SqlScriptUtils.convertForeach("item", "list",
    //                         field.getColumn() + " WHEN #{item." + keyProperty + "} THEN #{item." + field.getProperty() + "}",
    //                         , "END", ""))
    //                 .collect(Collectors.joining(",\n"));
    //
    //         // 构建 WHERE IN 部分的主键列表
    //         String whereInSql = SqlScriptUtils.convertForeach("item", "list", "#{item." + keyProperty + "}", ",", "", "");
    //
    //         // 拼接完整 SQL
    //         String finalSql = String.format(sql, tableName, setSql, keyColumn, whereInSql);
    //         // 构建动态 SQL 源
    //         SqlSource sqlSource = languageDriver.createSqlSource(configuration, finalSql, modelClass);
    //
    //         // 返回 MappedStatement
    //         return this.addUpdateMappedStatement(mapperClass, modelClass, "updateAllBatch", sqlSource);
    //     }
    // }
    //

}

